package com.ncloud.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.system.entity.ComputerRoom;
import com.ncloud.system.mapper.ComputerRoomMapper;
import com.ncloud.system.mapper.SequenceMapper;
import com.ncloud.system.service.ComputerRoomService;
@Service
public class ComputerRoomServiceImpl implements ComputerRoomService{
	@Resource
	private ComputerRoomMapper computerRoomMapper;
	@Resource
	private SequenceMapper sequenceMapper;
	@Override
	public JSONArray getComputerRoomInfo(JSONObject json) {
		List<JSONObject> computerRooms = computerRoomMapper.getComputerRoomInfo(json.getString("areaId"),json.getString("countryId"),json.getString("provinceId"),json.getString("cityId"));
		JSONArray results=JSON.parseArray(JSON.toJSONString(computerRooms));
		return results;
	}

	@Override
	public int insertComputerRoom(ComputerRoom computerRoom) {
		Integer roomId = sequenceMapper.getSequence("room_id").intValue();
		computerRoom.setRoomId(roomId);
		return computerRoomMapper.insertComputerRoom(computerRoom);
	}

	@Override
	public int updateComputerRoom(ComputerRoom computerRoom) {
		return computerRoomMapper.updateComputerRoom(computerRoom);
	}

	@Override
	public int deleteComputerRoom(Integer roomId) {
		List<JSONObject> cabinets = computerRoomMapper.getCabinetByRoomId(roomId);
		if(cabinets!=null&&cabinets.size()>0){
			throw new UserException(4000,"该机房存在机柜信息,不能删除!");
		}
		return computerRoomMapper.deleteComputerRoom(roomId);
	}
}
